package pl.edu.pjwstk.kor.sbql4j.project2;

import pl.wcislo.sbql4j.db4o.Db4oSBQLQuery;
import com.db4o.*;
import com.db4o.ObjectContainer;

import com.db4o.foundation.*;

import com.db4o.internal.*;
import com.db4o.internal.btree.*;

import org.apache.commons.collections.CollectionUtils;

import pl.edu.pjwstk.kor.database.Database;
import pl.edu.pjwstk.kor.database.Sbql4jQuery;
import pl.edu.pjwstk.kor.model.Customer;
import pl.edu.pjwstk.kor.model.Deal;
import pl.edu.pjwstk.kor.model.Employee;
import pl.edu.pjwstk.kor.model.Rank;

import pl.wcislo.sbql4j.exception.*;
import pl.wcislo.sbql4j.java.model.runtime.*;
import pl.wcislo.sbql4j.java.model.runtime.factory.*;
import pl.wcislo.sbql4j.java.utils.ArrayUtils;
import pl.wcislo.sbql4j.java.utils.OperatorUtils;
import pl.wcislo.sbql4j.java.utils.Pair;
import pl.wcislo.sbql4j.lang.codegen.nostacks.*;
import pl.wcislo.sbql4j.lang.codegen.simple.*;




import pl.wcislo.sbql4j.lang.parser.expression.*;
import pl.wcislo.sbql4j.lang.parser.expression.OrderByParamExpression.SortType;
import pl.wcislo.sbql4j.lang.parser.terminals.*;
import pl.wcislo.sbql4j.lang.parser.terminals.operators.*;
import pl.wcislo.sbql4j.lang.types.*;
import pl.wcislo.sbql4j.lang.xml.*;
import pl.wcislo.sbql4j.model.*;
import pl.wcislo.sbql4j.model.collections.*;
import pl.wcislo.sbql4j.util.*;
import pl.wcislo.sbql4j.util.Utils;
import pl.wcislo.sbql4j.xml.model.*;
import pl.wcislo.sbql4j.xml.parser.store.*;

import java.util.*;
import java.util.Collection;


public class Project2Query_SbqlQuery2Db4o0 implements Db4oSBQLQuery {
    private java.lang.String jobName;
    private java.lang.Integer dealYear;

    public Project2Query_SbqlQuery2Db4o0(java.lang.String jobName,
        java.lang.Integer dealYear) {
        this.jobName = jobName;
        this.dealYear = dealYear;
    }

    /**
     * query='db.(Deal as d where d.getEmployee().getJob() == jobName and d.getDealDate().getYear() >= dealYear)'
    '
     **/
    public java.util.Collection<pl.edu.pjwstk.kor.model.Deal> executeQuery(
        final ObjectContainerBase ocb, final Transaction t) {
        final LocalTransaction transLocal = (LocalTransaction) t;
        final java.util.Collection<pl.edu.pjwstk.kor.model.Deal> _ident_Deal = new java.util.ArrayList<pl.edu.pjwstk.kor.model.Deal>();
        ClassMetadata _classMeta2 = ocb.classCollection()
                                       .getClassMetadata("pl.edu.pjwstk.kor.model.Deal");
        long[] _ids2 = _classMeta2.getIDs(transLocal);

        for (long _id2 : _ids2) {
            LazyObjectReference _ref2 = transLocal.lazyReferenceFor((int) _id2);
            _ident_Deal.add((pl.edu.pjwstk.kor.model.Deal) _ref2.getObject());
        }

        java.util.Collection<pl.edu.pjwstk.kor.model.Deal> _asResult_d = _ident_Deal;
        java.util.Collection<pl.edu.pjwstk.kor.model.Deal> _whereResult = new java.util.ArrayList<pl.edu.pjwstk.kor.model.Deal>();
        int _whereLoopIndex = 0;

        for (pl.edu.pjwstk.kor.model.Deal _whereEl : _asResult_d) {
            if (_whereEl == null) {
                continue;
            }

            if (_whereEl != null) {
                ocb.activate(_whereEl, 1);
            }

            pl.edu.pjwstk.kor.model.Deal _ident_d = _whereEl;

            if (_ident_d != null) {
                ocb.activate(_ident_d, 1);
            }

            pl.edu.pjwstk.kor.model.Deal _dotEl = _ident_d;

            if (_ident_d != null) {
                ocb.activate(_ident_d, 2);
            }

            pl.edu.pjwstk.kor.model.Employee _mth_getEmployeeResult = _dotEl.getEmployee();

            if (_mth_getEmployeeResult != null) {
                ocb.activate(_mth_getEmployeeResult, 1);
            }

            pl.edu.pjwstk.kor.model.Employee _dotEl1 = _mth_getEmployeeResult;

            if (_mth_getEmployeeResult != null) {
                ocb.activate(_mth_getEmployeeResult, 2);
            }

            java.lang.String _mth_getJobResult = _dotEl1.getJob();

            if (_mth_getJobResult != null) {
                ocb.activate(_mth_getJobResult, 1);
            }

            java.lang.String _ident_jobName = jobName;
            java.lang.Boolean _equalsResult = OperatorUtils.equalsSafe(_mth_getJobResult,
                    _ident_jobName);
            java.lang.Boolean _andResult;

            if (!_equalsResult) {
                _andResult = false;
            } else {
                pl.edu.pjwstk.kor.model.Deal _ident_d1 = _whereEl;

                if (_ident_d1 != null) {
                    ocb.activate(_ident_d1, 1);
                }

                pl.edu.pjwstk.kor.model.Deal _dotEl2 = _ident_d1;

                if (_ident_d1 != null) {
                    ocb.activate(_ident_d1, 2);
                }

                pl.edu.pjwstk.kor.model.CustomDate _mth_getDealDateResult = _dotEl2.getDealDate();

                if (_mth_getDealDateResult != null) {
                    ocb.activate(_mth_getDealDateResult, 1);
                }

                pl.edu.pjwstk.kor.model.CustomDate _dotEl3 = _mth_getDealDateResult;

                if (_mth_getDealDateResult != null) {
                    ocb.activate(_mth_getDealDateResult, 2);
                }

                java.lang.Integer _mth_getYearResult = _dotEl3.getYear();

                if (_mth_getYearResult != null) {
                    ocb.activate(_mth_getYearResult, 1);
                }

                java.lang.Integer _ident_dealYear = dealYear;

                Boolean _more_or_equalResult = _mth_getYearResult >= _ident_dealYear;
                _andResult = _more_or_equalResult;
            }

            if (_andResult) {
                _whereResult.add(_whereEl);
            }

            _whereLoopIndex++;
        }

        pl.wcislo.sbql4j.db4o.utils.DerefUtils.activateResult(_whereResult, ocb);

        return _whereResult;
    }
}
